package com.NeuglsWorkStudio.Utility.Share
{
	import com.NeuglsWorkStudio.UI.FormElement.Component.TPopupMenu;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TPopupMenuConfig;
	import com.addthis.share.ShareAPI;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.system.System;

	public class TShare extends Object
	{
		public function TShare(){
			//Security.allowDomain('*');
			//Security.allowInsecureDomain('*');
			super();
		}
		
		//End point constants
		private static const BASE_DIR:String = 'http://cache.addthis.com/downloads/demo/flash-menu-lib/';
		public static const IMAGE_ENDPOINT:String = BASE_DIR + 'images/';
		
		
		public static var EmailCaption:String='Email a Friend';
		public static var CopyLink:String='Copy link address';
		public static var CopyHTMLCode:String='Copy html code';
		public static var MoreShare:String='More...';
		
		private static const idLink:String='link';
		private static const idCode:String='code';
	
		
		/**
		 * Sharing services - Menu takes services from here to display 
		 **/
		public static var SHARING_LIST:Array;
		
		private static function BuildShareObject():Array{
			return [
				{
					Caption : EmailCaption,
					id : 'email'
				},
				{
					Caption : "Facebook",
					id : "facebook"
				},
				{
					Caption : "iGoogle",
					id : "igoogle"
				},
				{
					Caption : "MySpace",
					id : "myspace"          
				},
				{
					Caption : "Netvibes",
					id : "netvibes"
				},
				{
					Caption : "Twitter",
					id : 'twitter'
				},
				{
					Caption : "Digg",
					id : 'digg'
				},
				{
					Caption : CopyLink,
					id : idLink
				},
				{
					Caption : CopyHTMLCode,
					id : idCode
				},
				{
					Caption : MoreShare,
					id : "more"          
				}
			];
		}
		
		private static function GetIconURL(S:String):String{
			return IMAGE_ENDPOINT + S + "_16.png";
		}
		
		private static function GetIcon(S:String):Bitmap{
			return new TShareImages[S] as Bitmap;
		}
		
		public static function ShowShare(UID:String,Caption:String,Title:String,Descript:String,ScreenShot:String,
										 PageURL:String,SWFURL:String,W:Number,H:Number,
			BackColor:int=0xBBBBBB,BackAlpha:Number=0.7):DisplayObject
		{
			SHARING_LIST=BuildShareObject();
			
			if(UID=='')
				UID='ra-4e0430ef67ab51a8';
			var shareParams:Object={
				'title'       : Title,
				'screenshot'  : ScreenShot,	
				'width'       : W,
				'height'      : H,
				'swfurl'      : SWFURL,
				'url'	  	  : PageURL,
				'description' : Descript
			};
			
			var MenuItems:Array=new Array();
			for(var i:Number=0;i<SHARING_LIST.length;i++){
				MenuItems.push({
					Caption:SHARING_LIST[i].Caption,
					ID:i,
					//IconURL:GetIconURL(SHARING_LIST[i].id),
					IconURL:'',
					UseIconURL:false,
					Icon:GetIcon(SHARING_LIST[i].id),
					ExtraData:null
				});
			}
			
			var R:TPopupMenu
			
			var PopupMenuConfig:TPopupMenuConfig=new TPopupMenuConfig();
			PopupMenuConfig.BackColor=BackColor;
			PopupMenuConfig.BackAlpha=BackAlpha;
			PopupMenuConfig.BorderColor=0xFFFFFF;
			PopupMenuConfig.BorderSize=1;
			PopupMenuConfig.RoundSize=10;
			PopupMenuConfig.MenuItems=MenuItems;
			PopupMenuConfig.Name='sharemenu';
			PopupMenuConfig.OnClickItem=function(ID:int,Extra:Object):void{
				switch(UID,SHARING_LIST[ID].id){
					case idLink:
						System.setClipboard(PageURL);
						break;
					case idCode:
						System.setClipboard('<object style="height: '+H+'px; width: '+W+'px"><param name="movie" value="'+SWFURL+'"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><embed src="'+SWFURL+'" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="'+W+'" height="'+H+'"></object>');
						break;
					default:
						var Share:ShareAPI=new ShareAPI(UID);			
						//SHARING_LIST[ID].id != "more" ? Share.share(PageURL,SHARING_LIST[ID].id,shareParams):Share.share(PageURL,'menu',shareParams);
						SHARING_LIST[ID].id != "more" ?ToShare(UID,SHARING_LIST[ID].id,shareParams):ToShare(UID,'menu',shareParams);
				}
				R.ToDisapperar();
			}
			PopupMenuConfig.Width=260;
			
			R=new TPopupMenu(PopupMenuConfig);
			return R;
		}
		

		
		
		public static function ToShare(UID:String,ID:String,ShareOption:Object):void{
			var ShareURL:String="http://api.addthis.com/oexchange/0.8/forward/"+ID+"/offer?"+
		    "&url="+encodeURI(ShareOption.url)+
		    "&title="+encodeURI(ShareOption.title)+
		    "&description="+encodeURI(ShareOption.description)+
		    "&swfurl="+encodeURI(ShareOption.swfurl)+
		    "&height="+ShareOption.height+
		    "&width="+ShareOption.width+
		    "&screenshot="+encodeURI(ShareOption.screenshot)+
			"&pubid="+encodeURI(UID);
			navigateToURL(new URLRequest(ShareURL),'_blank');
		}
		
		
		public static function SetCaption(Email:String,More:String,Link:String,Code:String):void{
			EmailCaption=Email;
			CopyLink=Link;
			CopyHTMLCode=Code;
			MoreShare=More;
		}
	}
}